package myaction.admin.courseinfo;

import myutil.DbUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class AdminCoursesAddAction extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String c_id = request.getParameter("c_id");
        String c_name = request.getParameter("c_name");
        String c_time = request.getParameter("c_time");
        String c_teacher_id = request.getParameter("c_teacher_id");
        try {
            Connection cn = DbUtil.getConn();
            String sql1 = "select c_id from t_courses where c_id = " + c_id + ";";
            Statement stmt2 = cn.createStatement();
            ResultSet rs = stmt2.executeQuery(sql1);
            if (rs.next()) {
                request.setAttribute("CourseRepeat", "编号为" + c_id + "的课程已存在！");
                request.getRequestDispatcher("AdminCoursesPreAddAction").forward(request, response);
            } else {
                String sql2 = "insert into t_courses(c_id,c_name,c_time,c_teacher_id,c_isable) values(?,?,?,?,1)";
                PreparedStatement ps = cn.prepareStatement(sql2);
                ps.setString(1, c_id);
                ps.setString(2, c_name);
                ps.setString(3, c_time);
                ps.setString(4, c_teacher_id);
                ps.executeUpdate();
                String sql3 = "select c_teacher_id from t_courses";
                //查询教师id是否为0，如果为0则不可选，如果为1则课程可选
                Statement st = cn.createStatement();
                ResultSet rs_1 = st.executeQuery(sql3);
                if (rs_1.next()) {
                    if (rs_1.getInt(1) == 0) {
                        String sql4 = "update t_courses set c_isable=? where c_teacher_id=?";
                        PreparedStatement ps1 = cn.prepareStatement(sql4);
                        ps1.setString(1, "0");
                        ps1.setString(2, String.valueOf(rs_1.getInt(1)));
                        ps1.executeUpdate();
                        ps1.close();
                    } else {
                        String sql5 = "update t_courses set c_isable=? where c_teacher_id=?";
                        PreparedStatement ps2 = cn.prepareStatement(sql5);
                        ps2.setString(1, "1");
                        ps2.setString(2, String.valueOf(rs_1.getInt(1)));
                        ps2.executeUpdate();
                        ps2.close();
                    }
                }
                ps.close();
                cn.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.sendRedirect("AdminCoursesFindAll");
    }
}
